package com.woniuxy.service.system.impl;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.RoleMapper;
import com.woniuxy.dao.RoleUserMapper;
import com.woniuxy.dao.UsersMapper;
import com.woniuxy.pojo.Role;
import com.woniuxy.pojo.RoleUserKey;
import com.woniuxy.pojo.Users;
import com.woniuxy.service.system.UserService;
import com.woniuxy.utils.MD5;
@Service
public class UserServiceImpl implements UserService{

	@Autowired
	private UsersMapper UsersMapper;
	@Autowired
	private RoleUserMapper roleUserMapper;
	@Autowired
	private RoleMapper roleMapper;
	
	@Override
	public PageInfo selectAll(Integer pn, String condition) {
		PageHelper.startPage(pn, 5);
		List<Users> list =UsersMapper.selectAll(condition);
		PageInfo pageInfo = new PageInfo(list,3);
		return pageInfo;
	}

	@Override
	public boolean nameExists(String userName) {
		Users user=UsersMapper.selectByUserName(userName);
		if (user==null) {
			return false;
		}else {
			return true;
		}
		
	}

	@Override
	public void addUser(Users user){
		user.setUserPassword(MD5.getMD5String(ByteSource.Util.bytes(user.getUserName()), "123456"));
		UsersMapper.insert(user);
	}

	@Override
	public Users selectById(Integer userId) {
		// TODO Auto-generated method stub
		return UsersMapper.selectByPrimaryKey(userId);
	}

	@Override
	public void updateUser(Users user) {
		UsersMapper.updateByPrimaryKey(user);
		
	}

	@Override
	public List<Role> selectRolesByUserId(Integer userId) {
		// TODO Auto-generated method stub
		return roleMapper.selectRolesByUserId(userId);
	}

	@Override
	public void assign(Integer[] roleIds, Integer userId) {
		roleUserMapper.deleteByUserId(userId);
		List<RoleUserKey> list = new ArrayList();
		if (roleIds!=null) {
			for (Integer roleId : roleIds) {
				RoleUserKey key = new RoleUserKey();
				key.setUserId(userId);
				key.setRoleId(roleId);
				list.add(key);
			}
			roleUserMapper.insertByKey(list);
		}
		
	}

	@Override
	public void deleteUser(Integer userId) {
		UsersMapper.deleteByPrimaryKey(userId);
		
	}

	@Override
	public Users isExist(String loginName, String newPassword) {
		// TODO Auto-generated method stub
		return UsersMapper.isExist(loginName,newPassword);
	}

	@Override
	public void resetPassword(String userName) {
		UsersMapper.resetPassword(userName,MD5.getMD5String(ByteSource.Util.bytes(userName), "123456"));
		
	}

	@Override
	public void updatePassword(String userName, String newPassword) {
		UsersMapper.resetPassword(userName, MD5.getMD5String(ByteSource.Util.bytes(userName), newPassword));		
	}

	@Override
	public boolean isAdmin(Integer userId) {
		Role role=roleMapper.isAdmin(userId);
		if (role==null) {
			return false;
		}
		return true;
	}



}
